package com.longrise.co.ui.login.model;

import com.google.gson.reflect.TypeToken;
import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.co.api.ApiConstants;
import com.longrise.co.api.ApiService;
import com.longrise.co.ui.login.contract.LoginContract;
import com.ly.common.basebean.BaseResponse;
import com.ly.common.baserx.RxResponseFuncForCar;

import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by luoyang on 2017-6-23.
 */

public class LoginModel implements LoginContract.Model {

    @Override
    public Observable<SmsCodeBean> sendSms(EntityBean bean) {
        return ApiService.getInstance().request(new TypeToken<BaseResponse<SmsCodeBean>>(){}.getType(), bean, ApiConstants.APPGETCODE)
                                        .subscribeOn(Schedulers.io())
                                        .observeOn(AndroidSchedulers.mainThread())
                                        .map(new RxResponseFuncForCar<SmsCodeBean>());
    }

    @Override
    public Observable<LoginBean> login(EntityBean bean) {
        return ApiService.getInstance().request(new TypeToken<BaseResponse<LoginBean>>(){}.getType(), bean, ApiConstants.APPCHECKLOGIN)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .map(new RxResponseFuncForCar<LoginBean>());
    }

    @Override
    public Observable<RegisterBean> register(EntityBean bean) {
        return ApiService.getInstance().request(new TypeToken<BaseResponse<RegisterBean>>(){}.getType(), bean, ApiConstants.APPREG)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .map(new RxResponseFuncForCar<RegisterBean>());
    }

    @Override
    public Observable<String> resetPwd(EntityBean bean) {
        return ApiService.getInstance().request(new TypeToken<BaseResponse<String>>(){}.getType(), bean, ApiConstants.APPRESETPASS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .map(new RxResponseFuncForCar<String>());
    }

    @Override
    public Observable<ImageValidateCode> getImageCode() {
        return ApiService.getInstance().request(new TypeToken<BaseResponse<ImageValidateCode>>(){}.getType(), new EntityBean(), ApiConstants.APPCODECREATER)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .map(new RxResponseFuncForCar<ImageValidateCode>());
    }
}
